/**
 * ===========================================
 * Project: qb-common
 * ===========================================
 * Package: com.cyou.qb.common.service.impl
 * 
 * Copyright (c) 2012, CYOU All Rights Reserved.
 * ===========================================
 */
package com.cyou.qb.common.service.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Service;

import com.cyou.nad.datastore.DataList;
import com.cyou.nad.datastore.query.JdbcPageDao;
import com.cyou.qb.common.dao.JiuGongGeDao;
import com.cyou.qb.common.domain.JiuGongGe;
import com.cyou.qb.common.domain.JiuGongGeEx;
import com.cyou.qb.common.service.ConfigDisService;
import com.cyou.qb.exception.ServiceException;

/**
 * <p>ConfigDisServiceImpl</p>
 * <p>
 * Description:配置摇奖盘service实现
 * </p>
 *
 * @author lidongwei
 * @version 
 * @since 2012-10-13
 */
@Service("ConfigDisServiceImpl")
public class ConfigDisServiceImpl extends BaseSerivceImpl implements ConfigDisService {

	@Resource
	JiuGongGeDao jiuGongGeDao;
	@Autowired
	private JdbcPageDao jdbcPageDao;
	@Autowired
	private JdbcTemplate jdbcTemplate;

	/**
	 * Description 保存一个活动的活动形式
	 * @author lidongwei
	 * @throws Exception 
	 * @since 2012-10-13 下午5:46:49
	 * @see TODOsome class
	 */
	@Override
	public void saveConfigDis(String activeId, List<String> awIds, List<String> pos) throws Exception {
		List<JiuGongGe> jiuGongGeList = new ArrayList<JiuGongGe>();
		//判断actIds和pos大小是否一致。
		if (awIds.size() != pos.size()) {
			throw new ServiceException("出错，获取的奖品id个数和位置个数不相同");
		}
		for (int i = 0; i < awIds.size(); i++) {
			JiuGongGe configDis = new JiuGongGe();
			configDis.setActiveId(Integer.valueOf(activeId));
			configDis.setAwardId(Integer.valueOf(awIds.get(i)));
			configDis.setCreateDate(new Date());
			configDis.setPosition(Integer.valueOf(pos.get(i)));
			jiuGongGeList.add(configDis);

		}
		if (jiuGongGeList.size() > 0) {
			//先删除后添加
			jiuGongGeDao.deleteJiuGongGeByActiveId(Integer.valueOf(activeId));
			jiuGongGeDao.saveJiuGongGeList(jiuGongGeList);
			getServiceContext().addLog(activeId, Integer.valueOf(activeId), "t_jiu_gong_ge", "添加九宫格信息",
					new Object[] { jiuGongGeList },
					new Object[] { jiuGongGeDao.findJIuGongGeByActiveId(Integer.valueOf(activeId)) });
		}


	}

	@Override
	public List<JiuGongGeEx> findJiuGongGeExByActId(int activeId) {
		String sql = "\n select a.active_id,a.image,b.position,a.name,a.id awardId from t_award a,t_jiu_gong_ge b"
				+ "\n where a.id= b.award_id" + "\n and b.active_id=?";
		List<JiuGongGeEx> ret = jdbcTemplate.query(sql, new Object[] { activeId }, new RowMapper<JiuGongGeEx>() {
			@Override
			public JiuGongGeEx mapRow(ResultSet rs, int rowNum) throws SQLException {
				JiuGongGeEx ex = new JiuGongGeEx();
				ex.setActiveId(rs.getInt("active_id"));
				ex.setAwardImage(rs.getString("image"));
				ex.setPosition(rs.getInt("position") - 1); //FIXME 前台配置 0-7；  后台配置 1-8
				ex.setAwardName(rs.getString("name"));
				ex.setTips(rs.getString("name"));
				ex.setAwardId(rs.getInt("awardId"));
				return ex;
			}
		});
		if (ret == null)
			ret = new ArrayList<JiuGongGeEx>();
		return ret;
	}

}
